package com.wiloon.java8x.thread.reorder;

/**
 * Created by wiloon on 3/26/17;
 * http://tech.meituan.com/java-memory-reordering.html
 */
public class PossibleReordering {
    static int x = 0, y = 0;
    static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        PossibleReordering possibleReordering = new PossibleReordering();
        for (int i = 0; i < 10; i++) {

            possibleReordering.run();
        }
    }


    public void run() throws InterruptedException {
        Thread one = new Thread(new Runnable() {
            public void run() {
                a = 1;
                x = b;
            }
        });

        Thread other = new Thread(new Runnable() {
            public void run() {
                b = 1;
                y = a;
            }
        });
        one.start();
        other.start();
        one.join();
        other.join();
        System.out.println("(" + x + "," + y + ")");
        if (x == 0 && y == 0) System.exit(0);
    }
}